<template>
  <a-modal
    title="单位选择"
    width="70%"
    :visible="visible"
    :confirmLoading="loading"
    okText="选择"
    @ok="handleChoose"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-row :gutter="20">
      <a-col :span="5">
        <!-- <a-input-search style="margin-bottom: 8px" placeholder="Search" /> -->

        <a-tree
          default-expand-all
          :treeData="treeData"
          @select="onSelect"
          :replaceFields="{ children: 'children', title: 'Name', key: 'Id' }"
          class="directorytree"
          :style="{ height: scroll + 38 + 40 + 'px', border: '1px solid #ccc', overflowY: 'scroll' }"
        >
        </a-tree>
      </a-col>
      <a-col :span="19">
        <a-row :gutter="10" style="margin-bottom: 8px;">
          <a-col :md="5" :sm="24">
            <a-input v-model="queryParam.Keyword" placeholder="单位编码/名称" />
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button
              style="margin-left: 8px"
              @click="() => ((queryParam = {}), (this.pagination.current = 1), getDataList())"
              >重置</a-button
            >
          </a-col>
        </a-row>
        <a-table
          :scroll="{ y: scroll }"
          :columns="columns"
          :rowKey="row => row.Id"
          :dataSource="data"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: type }"
          :bordered="true"
          size="small"
        >
          <template slot="dataIndex" slot-scope="text, record, index">
            <span>{{ (pagination.current - 1) * pagination.pageSize + index + 1 }}</span>
          </template>
        </a-table>
      </a-col>
    </a-row>
    <!-- <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="5" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.KeyWord" placeholder="单位编码/名称" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div> -->
  </a-modal>
</template>

<script>
// import EnumName from '../BaseEnum/BaseEnumName'
// import EnumSelect from '../BaseEnum/BaseEnumSelect'
const columns = [
  {
    title: '序号',
    dataIndex: 'aId',
    scopedSlots: { customRender: 'dataIndex' },
    width: '50px'
  },
  { title: '计量单位编码', dataIndex: 'Code' },
  { title: '计量单位名称', dataIndex: 'Name' }
]

export default {
  props: {
    type: { type: String, default: 'radio', required: false }
  },
  components: {
    // EnumName,
    // EnumSelect
  },
  // mounted() {
  //   this.getDataList()
  // },
  data() {
    return {
      treeData: [],
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: { Keyword: '' },
      selectedRowKeys: [],
      selectedRows: [],
      visible: false,
      Keyword: ''
    }
  },
  computed: {
    scroll() {
      return Number(document.documentElement.clientHeight) * 0.5
    }
  },
  methods: {
    gettreeData() {
      this.$http.post('/Base_BasicData/Base_UnitGroup/GetDataList', {}).then(res => {
        res.Data.forEach(item => {
          item.Name = `${item.Name}  (${item.Code})`
        })
        this.treeData = res.Data
      })
    },
    onSelect(keys, event) {
      this.Keyword = keys[0]
      this.pagination.current = 1
      this.pagination.pageSize = 25
      this.queryParam.Keyword = ''
      this.getDataList()
    },
    onExpand() {
      console.log('Trigger Expand')
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []
      this.selectedRows = []

      this.loading = true
      this.$http
        .post('/Base_BasicData/Base_UnitGroup/GetUnitItemsListAsyncs', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: {
            UnitGroupId: this.Keyword,
            Keyword: this.queryParam.Keyword
          },
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = [...resJson.Data]

          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    openChoose() {
      this.gettreeData()
      // this.getDataList()
      this.visible = true
    },
    handleChoose() {
      console.log(this.selectedRows)
      this.visible = false
      this.$emit('onChoose', this.selectedRows)
    }
  }
}
</script>
